package com.qyhl.module_activities.act.comment;


import com.google.gson.Gson;
import com.qyhl.module_activities.common.ActUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ErrorCode;
import com.qyhl.webtv.commonlib.entity.act.ActCommentBean;

import java.util.List;

/**
 * Created by qyhl2 on 2018/1/22.
 */

public class CommentListModel implements CommentListContract.CommentListModel {

    private final CommentListContract.CommentListPresenter presenter;

    public CommentListModel(CommentListContract.CommentListPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getComment(int actId, final int commentId) {

        EasyHttp.post(ActUrl.ACTIVITY_COMMENT_LIST)
                .params("actId", actId + "")
                .params("commentId", commentId + "")
                .execute(new SimpleCallBack<List<ActCommentBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.getCommentError("获取数据失败", commentId == 0);
                    }

                    @Override
                    public void onSuccess(List<ActCommentBean> actCommentBeans) {
                        presenter.onSuccess(actCommentBeans, commentId == 0);
                    }
                });

    }

    @Override
    public void sendComment(int actId, String content) {
        String nickName = CommonUtils.getInstance().getNickName();
        String headicon = CommonUtils.getInstance().getUserLogo();
        String phoneNum = CommonUtils.getInstance().getUserName();

        EasyHttp.post(ActUrl.ACTIVITY_ADD_COMMENT)
                .params("content", content)
                .params("actId", actId + "")
                .params("phone", phoneNum)
                .params("nickName", nickName)
                .params("avatar", headicon)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.commentError("无网络连接，请检查您的网络...");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult= new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            presenter.commentSuccess(true, "评论成功");
                        } else if (apiResult.getCode() == ErrorCode.ACTIVITY_COMMENT_RULE) {
                            presenter.commentSuccess(false, "评论成功,请等待审核");
                        } else {
                            presenter.commentError("评论失败");
                        }
                    }
                });

    }
}
